using System.Collections;
using UnityEngine;

namespace PKGames
{
	[CreateAssetMenu(fileName = "FadeTransition", menuName = "Window Transitions/FadeTransition", order = 0)]
	public class FadeTransition : WindowTransition
	{
		[SerializeField]
		private AnimationCurve _fadeCurve;

		[SerializeField]
		private float _animationTime = 1f;

		public override IEnumerator BeginOpenWindowTransition(RectTransform targetTransform)
		{
			_nextWindow.transform.position = targetTransform.position;
			_nextWindow.WindowCanvasGroup().alpha = 0f;
			_nextWindow.WindowHolder.SetActive(value: true);
			yield return FadeInNextWindow();
			yield return base.BeginOpenWindowTransition(targetTransform);
		}

		private IEnumerator FadeInNextWindow()
		{
			for (float time = 0f; time < _animationTime; time += Time.deltaTime)
			{
				_nextWindow.WindowCanvasGroup().alpha = _fadeCurve.Evaluate(time / _animationTime);
				yield return new WaitForEndOfFrame();
			}
			_nextWindow.WindowCanvasGroup().alpha = 1f;
		}

		public override IEnumerator BeginCloseWindowTransition(RectTransform targetTransform)
		{
			if (_nextWindow != null)
			{
				_nextWindow.transform.position = targetTransform.position;
			}
			yield return FadeOutLastWindow();
			yield return base.BeginCloseWindowTransition(targetTransform);
		}

		private IEnumerator FadeOutLastWindow()
		{
			for (float time = _animationTime; time > 0f; time -= Time.deltaTime)
			{
				_lastWindow.WindowCanvasGroup().alpha = _fadeCurve.Evaluate(time / _animationTime);
				yield return new WaitForEndOfFrame();
			}
			_lastWindow.WindowCanvasGroup().alpha = 0f;
		}
	}
}
